
import { _decorator, Node, Label, view, UITransform, Sprite, find} from 'cc';
import { App } from '../../App';
import CommConst from '../../const/CommConst';
import wsTool from '../../net/ws';
import { PD } from '../../player/PlayerData';
import { loadCommonPreFab, setLanguagePopuUp, showErrorTip, showTip, showTipBox } from '../../tools/PopuUp';
import UtilTool from '../../tools/UtilTool';
import AbstractComponent from '../../base/AbstractComponent';
const { ccclass, property } = _decorator;

// 我的球杆界面
@ccclass('MineCuePanel')
export class MineCuePanel extends AbstractComponent {
    @property(Node)
    bg: Node = null

    parentObj = null
    cueData = null

    onLoad() {
        UtilTool.setScreen(this.node)
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())

        UtilTool.nodeActive(this.bg, 5)
    }

    start() {

    }

    setData(obj, data) {
        this.parentObj = obj
        this.cueData = data

        let nameLabel = find("nameLabel", this.bg).getComponent(Label)   // 名字
        let cueSprite = find("cueSprite", this.bg).getComponent(Sprite)  // 球杆图片
        let coinLabel = find("coinLabel", this.bg)                       // 金币
        let coinSprite = find("coinSprite", this.bg)                     // 金币图片

        nameLabel.string = App.language.getLanguageLab(176)[data.number + ""]
        cueSprite.spriteFrame = this.parentObj.cueFrames[data.number]
        coinLabel.getComponent(Label).string = data.gold + ""
        UtilTool.adaptiveLabel([coinLabel,coinSprite], 0, 10)
    }

    /**
     * 确定购买
     */
    clickSure() {
        App.Audio.playOneShot("click")

        if (this.cueData.gold > PD.user.coin){
            showTip(App.language.getLanguageLab(30))
            return
        }

        wsTool.send({msgCode:CommConst.Req_BUY_CUE, "number":this.cueData.number})
        .then((data) => {
            if(data.result == 0) {
                showTip(App.language.getLanguageLab(59))
                this.node.destroy()
                this.parentObj.reqListData()

            } else {
                showErrorTip(data.result)
            }
        })
    }

    /**
     * 取消购买
     */
    clickCancel() {
        App.Audio.playOneShot("click")
        this.node.destroy()
    }

}

